package com.txf.other_playerlibrary.ui.player.control;

import android.content.Context;
import android.content.res.Configuration;
import android.hardware.SensorManager;
import android.util.AttributeSet;
import android.view.OrientationEventListener;
import android.view.View;
import android.view.ViewGroup;

import com.txf.other_playerlibrary.R;
import com.txf.other_playerlibrary.interfaces.IPlayerControl;
import com.txf.other_playerlibrary.interfaces.IPlayerControlView;
import com.txf.other_playerlibrary.tools.Logger;
import com.txf.other_playerlibrary.ui.player.base.BaseViewGroup;

/**
 * @author txf
 * @create 2019/2/14 0014
 */
public class PlayerControlView extends BaseViewGroup implements IPlayerControlView {
    IPlayerControl mIPlayerControl;
    OrientationEventListener mOrientationListener;

    public PlayerControlView(Context context) {
        super(context);
    }

    public PlayerControlView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PlayerControlView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    protected void onCreate() {
        View.inflate(getContext(), R.layout.view_playercontrol, this);
        mOrientationListener = new OrientationEventListener(getContext(), SensorManager.SENSOR_DELAY_NORMAL) {
            @Override
            public void onOrientationChanged(int orientation) {
                Logger.show("屏幕角度发生改变: " + orientation);
                if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
                    return;  //手机平放时，检测不到有效的角度
                }
                //只检测是否有四个角度的改变
                if (orientation > 350 || orientation < 10) { //0度
                    orientation = 0;

                } else if (orientation > 80 && orientation < 100) { //90度
                    orientation = 90;

                } else if (orientation > 170 && orientation < 190) { //180度
                    orientation = 180;

                } else if (orientation > 260 && orientation < 280) { //270度
                    orientation = 270;

                }
            }
        };
        if (mOrientationListener.canDetectOrientation()) {
            Logger.show("屏幕角度侦听器注册");
            mOrientationListener.enable();
        } else {
            Logger.show("屏幕角度侦听器反注册");
            mOrientationListener.disable();
        }
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public LayoutParams getViewLayoutParams() {
        return new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    @Override
    public void setIPlayerControl(IPlayerControl playerControl) {
        mIPlayerControl = playerControl;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mOrientationListener != null)
            mOrientationListener.disable();
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
}
